#include "Sphere.h"
#include "Ray.h"
#include <math.h>

//bool Sphere::Intersect(D3DXVECTOR3 ray)
//{
//	return false;
//}

float Sphere::Intersect(RAY &ray) 
{
	D3DXVECTOR3 dst = ray.origin - this->center;
	float B = D3DXVec3Dot(&dst, &ray.direction);
	float C = D3DXVec3Dot(&dst, &dst) - (this->radius );
	float D = B*B - C;
	return D > 0.0f ? -B - sqrt(D) :  FLT_MAX;
}

//float Sphere::Intersect(RAY &ray) 
//{
//	float B = 2 * ((ray.direction.x * (ray.origin.x - this->center.x)) + (ray.direction.y * (ray.origin.y - this->center.y)) + (ray.direction.z * (ray.origin.z - this->center.z)));
//	float C = ::pow(ray.origin.x - this->center.x,2.0f) + ::pow(ray.origin.y - this->center.y,2.0f) + ::pow(ray.origin.z - this->center.z,2.0f) - ::pow(this->radius,2.0f);
//	float root = ::pow(B,2.0f) - (4 * C);
//	float t = FLT_MAX;
//	if(root >= 0)
//	{
//		t = (-B + ::sqrt(root))/2.0f;
//	}
//	return t;
//}

void Sphere::Normal(D3DXVECTOR3* output, D3DXVECTOR3 &point)
{
	::D3DXVec3Normalize(output, &(point - this->center)); 
}